package com.example.demo.config;


import com.example.demo.common.AjaxResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *  统一异常处理
 */
@ControllerAdvice
@ResponseBody
public class MyExHandler {
    /**
     * 拦截所有的空指针异常, 进行统一的数据返回.
     */
    @ExceptionHandler(NullPointerException.class)
    public AjaxResult nullException(NullPointerException e) {
        return AjaxResult.fail(-3, "空指针异常: " + e.getMessage());
    }

    /**
     * 拦截所有的算数异常, 进行统一的数据返回.
     */
    @ExceptionHandler(ArithmeticException.class)
    public AjaxResult numberException(ArithmeticException e) {
        return AjaxResult.fail(-3, "算数异常: " + e.getMessage());
    }

    /**
     *  保底处理异常
     */
    @ExceptionHandler(Exception.class)
    public AjaxResult exception(Exception e) {
        return AjaxResult.fail(-3, "异常: " + e.getMessage());
    }
}
